package com.jtech.together.view.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.jtech.together.R;
import com.jtech.together.mvp.contract.MainContract;
import com.jtech.together.mvp.presenter.MainPresenter;
import com.jtech.together.service.LocationService;
import com.jtech.together.view.activity.base.BaseActivity;
import com.jtech.together.view.fragment.FriendsFragment;
import com.jtech.together.view.fragment.HomeFragment;
import com.jtech.together.view.fragment.MessageFragment;
import com.jtech.together.view.fragment.MyProfileFragment;
import com.jtech.together.view.fragment.base.BaseFragment;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 主页
 * Created by wuxubaiyang on 2017/5/10.
 */
public class MainActivity extends BaseActivity implements MainContract.View {
    @Bind(R.id.linearlayout_main_tabs)
    LinearLayout linearLayoutTabs;

    private MainContract.Presenter presenter;
    private List<BaseFragment> baseFragments;
    private List<ImageButton> tabs;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new MainPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        //初始化fragment
        initFragments();
        //初始化导航栏
        initTabs();
    }

    @Override
    protected void loadData() {
        //开启定位服务
        startLocationService();
        //连接到服务器
        presenter.connectIM();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    /**
     * 初始化fragment
     */
    private void initFragments() {
        baseFragments = new ArrayList<>();
        baseFragments.add(HomeFragment.newInstance());
        baseFragments.add(MessageFragment.newInstance());
        baseFragments.add(FriendsFragment.newInstance());
        baseFragments.add(MyProfileFragment.newInstance());
        //添加到fragmentManager
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        for (int i = 0; i < baseFragments.size(); i++) {
            transaction.add(R.id.framelayout_main, baseFragments.get(i));
        }
        transaction.commit();
    }

    /**
     * 初始化底部导航栏
     */
    private void initTabs() {
        tabs = new ArrayList<>();
        for (int i = 0; i < linearLayoutTabs.getChildCount(); i++) {
            final int j = i;
            ImageButton tab = (ImageButton) linearLayoutTabs.getChildAt(i);
            tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //默认选中
                    fragmentSelect(j);
                    tabSelect(j);
                }
            });
            tabs.add(tab);
        }
        //默认选中
        fragmentSelect(0);
        tabSelect(0);
    }

    /**
     * 开启定位服务
     */
    private void startLocationService() {
        Dexter.withActivity(getActivity())
                .withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        //启动定位服务
                        startService(new Intent(getActivity(), LocationService.class));
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                }).check();
    }

    /**
     * fragment的显示隐藏
     *
     * @param index
     */
    private void fragmentSelect(int index) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        for (int i = 0; i < baseFragments.size(); i++) {
            if (i == index) {
                transaction.show(baseFragments.get(i));
            } else {
                transaction.hide(baseFragments.get(i));
            }
        }
        transaction.commit();
    }

    /**
     * tab选择
     *
     * @param index
     */
    private void tabSelect(int index) {
        for (int i = 0; i < tabs.size(); i++) {
            tabs.get(i).setSelected(i == index);
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}